﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=Web company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datWeb class
    /// </summary>

    public class datWeb
    {
        public datWeb()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa Web===
        public void createEdit(entWeb entW)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", entW.Id);
            cmd.Parameters.AddWithValue("Ten", entW.Ten);
            cmd.Parameters.AddWithValue("SiteUrl", entW.SiteUrl);
            cmd.Parameters.AddWithValue("Url", entW.Url);
            cmd.Parameters.AddWithValue("RootDiv", entW.RootDiv);
            cmd.Parameters.AddWithValue("NodeName", entW.NodeName);
            cmd.Parameters.AddWithValue("NodeNameAttribute", entW.NodeNameAttribute);
            cmd.Parameters.AddWithValue("NodeImage", entW.NodeImage);
            cmd.Parameters.AddWithValue("NodeImageAttribute", entW.NodeImageAttribute);
            cmd.Parameters.AddWithValue("NodeLink", entW.NodeLink);
            cmd.Parameters.AddWithValue("NodeLinkAttribute", entW.NodeLinkAttribute);
            cmd.Parameters.AddWithValue("NodeGiaNiemYet", entW.NodeGiaNiemYet);
            cmd.Parameters.AddWithValue("NodeGiaNiemYetAttribute", entW.NodeGiaNiemYetAttribute);
            cmd.Parameters.AddWithValue("NodeGiaKM", entW.NodeGiaKM);
            cmd.Parameters.AddWithValue("NodeGiaKMAttribute", entW.NodeGiaKMAttribute);
            cmd.Parameters.AddWithValue("NodeThoiGian", entW.NodeThoiGian);
            cmd.Parameters.AddWithValue("NodeThoiGianAttribute", entW.NodeThoiGianAttribute);
            cmd.Parameters.AddWithValue("NodeNoiDung", entW.NodeNoiDung);
            cmd.Parameters.AddWithValue("NodeNoiDungAttribute", entW.NodeNoiDungAttribute);
            cmd.Parameters.AddWithValue("WebChaId", entW.WebChaId);
            cmd.Parameters.AddWithValue("TrangThai", entW.TrangThai);
            cmd.Parameters.AddWithValue("NguoiTao", entW.NguoiTao);
            cmd.Parameters.AddWithValue("NguoiSua", entW.NguoiSua);
            cmd.Parameters.AddWithValue("NgayTao", entW.NgayTao);
            cmd.Parameters.AddWithValue("NgaySua", entW.NgaySua);

            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall Web===
        public List<entWeb> getAll(ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entWeb entW = new entWeb();
            List<entWeb> lst = new List<entWeb>();
            while (dr.Read())
            {
                entW = new entWeb();
                entW.Id = Convert.ToInt32(dr["Id"].ToString());
                entW.Ten = dr["Ten"].ToString();
                entW.SiteUrl = dr["SiteUrl"].ToString();
                entW.Url = dr["Url"].ToString();
                entW.RootDiv = dr["RootDiv"].ToString();
                entW.NodeName = dr["NodeName"].ToString();
                entW.NodeNameAttribute = dr["NodeNameAttribute"].ToString();
                entW.NodeImage = dr["NodeImage"].ToString();
                entW.NodeImageAttribute = dr["NodeImageAttribute"].ToString();
                entW.NodeLink = dr["NodeLink"].ToString();
                entW.NodeLinkAttribute = dr["NodeLinkAttribute"].ToString();
                entW.NodeGiaNiemYet = dr["NodeGiaNiemYet"].ToString();
                entW.NodeGiaNiemYetAttribute = dr["NodeGiaNiemYetAttribute"].ToString();
                entW.NodeGiaKM = dr["NodeGiaKM"].ToString();
                entW.NodeGiaKMAttribute = dr["NodeGiaKMAttribute"].ToString();
                entW.NodeThoiGian = dr["NodeThoiGian"].ToString();
                entW.NodeThoiGianAttribute = dr["NodeThoiGianAttribute"].ToString();
                entW.NodeNoiDung = dr["NodeNoiDung"].ToString();
                entW.NodeNoiDungAttribute = dr["NodeNoiDungAttribute"].ToString();
                entW.WebChaId = Convert.ToInt32(dr["WebChaId"].ToString());
                entW.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entW.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entW.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entW.NgayTao = dr["NgayTao"].ToString();
                entW.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entW);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall Web===
        public List<entWeb> getByIdParent(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebGetAllByIdParent", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Id", Id);
            dr = cmd.ExecuteReader();
            entWeb entW = new entWeb();
            List<entWeb> lst = new List<entWeb>();
            while (dr.Read())
            {
                entW = new entWeb();
                entW.Id = Convert.ToInt32(dr["Id"].ToString());
                entW.Ten = dr["Ten"].ToString();
                entW.SiteUrl = dr["SiteUrl"].ToString();
                entW.Url = dr["Url"].ToString();
                entW.RootDiv = dr["RootDiv"].ToString();
                entW.NodeName = dr["NodeName"].ToString();
                entW.NodeNameAttribute = dr["NodeNameAttribute"].ToString();
                entW.NodeImage = dr["NodeImage"].ToString();
                entW.NodeImageAttribute = dr["NodeImageAttribute"].ToString();
                entW.NodeLink = dr["NodeLink"].ToString();
                entW.NodeLinkAttribute = dr["NodeLinkAttribute"].ToString();
                entW.NodeGiaNiemYet = dr["NodeGiaNiemYet"].ToString();
                entW.NodeGiaNiemYetAttribute = dr["NodeGiaNiemYetAttribute"].ToString();
                entW.NodeGiaKM = dr["NodeGiaKM"].ToString();
                entW.NodeGiaKMAttribute = dr["NodeGiaKMAttribute"].ToString();
                entW.NodeThoiGian = dr["NodeThoiGian"].ToString();
                entW.NodeThoiGianAttribute = dr["NodeThoiGianAttribute"].ToString();
                entW.NodeNoiDung = dr["NodeNoiDung"].ToString();
                entW.NodeNoiDungAttribute = dr["NodeNoiDungAttribute"].ToString();
                //entW.WebChaId = Convert.ToInt32(dr["WebChaId"].ToString());
                //entW.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                //entW.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                //entW.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                //entW.NgayTao = dr["NgayTao"].ToString();
                //entW.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entW);
            }

            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall Web===
        public List<entWeb> getAllByForeignKey(int Id, ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebGetAllByForeignKey", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entWeb entW = new entWeb();
            List<entWeb> lst = new List<entWeb>();
            while (dr.Read())
            {
                entW = new entWeb();
                entW.Id = Convert.ToInt32(dr["Id"].ToString());
                entW.Ten = dr["Ten"].ToString();
                entW.SiteUrl = dr["SiteUrl"].ToString();
                entW.Url = dr["Url"].ToString();
                entW.RootDiv = dr["RootDiv"].ToString();
                entW.NodeName = dr["NodeName"].ToString();
                entW.NodeNameAttribute = dr["NodeNameAttribute"].ToString();
                entW.NodeImage = dr["NodeImage"].ToString();
                entW.NodeImageAttribute = dr["NodeImageAttribute"].ToString();
                entW.NodeLink = dr["NodeLink"].ToString();
                entW.NodeLinkAttribute = dr["NodeLinkAttribute"].ToString();
                entW.NodeGiaNiemYet = dr["NodeGiaNiemYet"].ToString();
                entW.NodeGiaNiemYetAttribute = dr["NodeGiaNiemYetAttribute"].ToString();
                entW.NodeGiaKM = dr["NodeGiaKM"].ToString();
                entW.NodeGiaKMAttribute = dr["NodeGiaKMAttribute"].ToString();
                entW.NodeThoiGian = dr["NodeThoiGian"].ToString();
                entW.NodeThoiGianAttribute = dr["NodeThoiGianAttribute"].ToString();
                entW.NodeNoiDung = dr["NodeNoiDung"].ToString();
                entW.NodeNoiDungAttribute = dr["NodeNoiDungAttribute"].ToString();
                entW.WebChaId = Convert.ToInt32(dr["WebChaId"].ToString());
                entW.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entW.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entW.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entW.NgayTao = dr["NgayTao"].ToString();
                entW.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entW);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo Web===
        public entWeb getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entWeb entW = new entWeb();
            if (dr.Read())
            {
                entW.Id = Convert.ToInt32(dr["Id"].ToString());
                entW.Ten = dr["Ten"].ToString();
                entW.SiteUrl = dr["SiteUrl"].ToString();
                entW.Url = dr["Url"].ToString();
                entW.RootDiv = dr["RootDiv"].ToString();
                entW.NodeName = dr["NodeName"].ToString();
                entW.NodeNameAttribute = dr["NodeNameAttribute"].ToString();
                entW.NodeImage = dr["NodeImage"].ToString();
                entW.NodeImageAttribute = dr["NodeImageAttribute"].ToString();
                entW.NodeLink = dr["NodeLink"].ToString();
                entW.NodeLinkAttribute = dr["NodeLinkAttribute"].ToString();
                entW.NodeGiaNiemYet = dr["NodeGiaNiemYet"].ToString();
                entW.NodeGiaNiemYetAttribute = dr["NodeGiaNiemYetAttribute"].ToString();
                entW.NodeGiaKM = dr["NodeGiaKM"].ToString();
                entW.NodeGiaKMAttribute = dr["NodeGiaKMAttribute"].ToString();
                entW.NodeThoiGian = dr["NodeThoiGian"].ToString();
                entW.NodeThoiGianAttribute = dr["NodeThoiGianAttribute"].ToString();
                entW.NodeNoiDung = dr["NodeNoiDung"].ToString();
                entW.LoaiKhuyenMaiId = int.Parse(dr["LoaiKhuyenMaiId"].ToString());
                entW.ThanhPho = int.Parse(dr["thanhpho"].ToString());
            }
            dr.Close();
            sqlConnection.Close();
            return entW;
        }
        #endregion

        #region ==get all web con
        public List<entWeb> getAllWebChild()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("select * from Web where WebChaId<>0", sqlConnection);

            dr = cmd.ExecuteReader();
            List<entWeb> lst = new List<entWeb>();
            entWeb entW = new entWeb();
            while (dr.Read())
            {
                entW = new entWeb();
                entW.Id = Convert.ToInt32(dr["Id"].ToString());
                entW.Ten = dr["Ten"].ToString();
                entW.SiteUrl = dr["SiteUrl"].ToString();
                entW.Url = dr["Url"].ToString();
                entW.RootDiv = dr["RootDiv"].ToString();
                entW.NodeName = dr["NodeName"].ToString();
                entW.NodeNameAttribute = dr["NodeNameAttribute"].ToString();
                entW.NodeImage = dr["NodeImage"].ToString();
                entW.NodeImageAttribute = dr["NodeImageAttribute"].ToString();
                entW.NodeLink = dr["NodeLink"].ToString();
                entW.NodeLinkAttribute = dr["NodeLinkAttribute"].ToString();
                entW.NodeGiaNiemYet = dr["NodeGiaNiemYet"].ToString();
                entW.NodeGiaNiemYetAttribute = dr["NodeGiaNiemYetAttribute"].ToString();
                entW.NodeGiaKM = dr["NodeGiaKM"].ToString();
                entW.NodeGiaKMAttribute = dr["NodeGiaKMAttribute"].ToString();
                entW.NodeThoiGian = dr["NodeThoiGian"].ToString();
                entW.NodeThoiGianAttribute = dr["NodeThoiGianAttribute"].ToString();
                entW.NodeNoiDung = dr["NodeNoiDung"].ToString();
                entW.LoaiKhuyenMaiId = int.Parse(dr["LoaiKhuyenMaiId"].ToString());
                entW.ThanhPho = int.Parse(dr["thanhpho"].ToString());
                lst.Add(entW);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;

        }

        #endregion

        #region === xoa Web===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_WebDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
